//
//  EpointIOUtil.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-1-22.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointIOUtil.h"

@implementation EpointIOUtil


+(void)openTxtFile:(NSString*)fileName withWebView:(UIWebView *)wv
{
    ///编码可以解决 .txt 中文显示乱码问题
    NSStringEncoding *useEncodeing = nil;
    //带编码头的如utf-8等，这里会识别出来
    NSString *body = [NSString stringWithContentsOfFile:fileName usedEncoding:useEncodeing error:nil];
    //识别不到，按GBK编码再解码一次.这里不能先按GB18030解码，否则会出现整个文档无换行bug。
    if (!body) {
        body = [NSString stringWithContentsOfFile:fileName encoding:0x80000632 error:nil];
    }
    //还是识别不到，按GB18030编码再解码一次.
    if (!body) {
        body = [NSString stringWithContentsOfFile:fileName encoding:0x80000631 error:nil];
    }
    //展现
    if (body) {
        [wv loadHTMLString:body baseURL: nil];
    }else {
        NSString *urlString = [[NSBundle mainBundle] pathForAuxiliaryExecutable:fileName];
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *requestUrl = [NSURL URLWithString:urlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:requestUrl];
        [wv loadRequest:request];
        
    }
}

+(BOOL) writeToFile:(NSString*)fileWithPath withData:(NSData*)data
{
    BOOL folder = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString* fileName = [fileWithPath lastPathComponent];
    NSInteger toIndex = [fileWithPath length] - [fileName length];
    NSString* path = [fileWithPath substringToIndex:toIndex];
    BOOL exist = [fileManager fileExistsAtPath:path isDirectory:&folder];
    if (  exist != YES ) {
        NSError* error = nil;
        [fileManager createDirectoryAtPath:path
               withIntermediateDirectories:YES
                                attributes:nil
                                     error:&error];
        if( error != nil ){
            NSLog(@"Can not create directory %@.  Error is %@.", path,
                  [error description]);
            return NO;
        }
    }
    if ( data != nil ) {
        return [data writeToFile:fileWithPath atomically:YES];
    }
    return NO;
}

//单个文件的大小
+(long long)fileSizeAtPath:(NSString*)filePath
{
//    return [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]fileSize];
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        long long size =[[manager attributesOfItemAtPath:filePath error:nil] fileSize];
        return size;
    }
    return 0;
}

//遍历文件夹获得文件夹大小，返回多少M
+(float ) folderSizeAtPath:(NSString*) folderPath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

+(void)deleteFile:(NSString*)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:filePath error:nil];
}

+(void)loadImageAtPath:(UIImageView *)iv path:(NSString*) filepath
{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSData *tmpContent = [fm contentsAtPath:filepath];
    [iv setImage:[UIImage imageWithData:tmpContent]];
}

@end
